package ch.ethz.fcl.metrobuzz.render.interchange;

import org.lwjgl.opengl.Display;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.MBProperty;
import ch.ethz.fcl.metrobuzz.data.singapore.SGInterchange;
import ch.ethz.fcl.metrobuzz.render.AbstractRenderer;
import ch.ethz.fcl.metrobuzz.render.interchange.mrt.MRTInterchangeRender;
import ch.ethz.fcl.metrobuzz.render.interchange.spatial.MultiscaleRender;
import ch.ethz.fcl.metrobuzz.render.interchange.temporal.TemporalRender;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Rectangle;

public class InterchangeRender extends AbstractRenderer {
	private MBContainer container;

	private boolean initializedResult = false;

	public static boolean UPDATE = true;

	private MRTInterchangeRender mrt = null;
	private MultiscaleRender multiscale = null;
	private TemporalRender tr = null;

	private int preLevel = 0;

	public InterchangeRender(MBContainer container) {
		this.container = container;
	}

	public void update() {
		if (!initializedResult) {
			init();
			initializedResult = true;
		}

		int level = SGInterchange.getLevel(container.getViewer().getCamera()
				.getDistance());

		if (level != preLevel) {
			preLevel = level;
			UPDATE = true;
		}

		if (UPDATE) {
			container.getData().getSGInfo().getSGInterchange().update();

			UPDATE = false;
		}

		render();
	}

	protected void render() {
		Rectangle bound = container.getViewer().getProjection()
				.getBound(0, 0, Display.getWidth(), Display.getHeight(), 0);
		if (MBProperty.MRT)
			mrt.render(bound);
		if (MBProperty.REGIONS)
			multiscale.render(bound);
		 tr.render(bound);
	}

	public void init() {
		if (container.getData() == null) {
			initializedResult = false;
			return;
		}

		if (container == null) {
			initializedResult = false;
			return;
		}

		if (MBProperty.MRT)
			mrt = new MRTInterchangeRender(this.container, container.getData()
					.getSGInfo().getSGInterchange());

		if (MBProperty.REGIONS)
			multiscale = new MultiscaleRender(this.container, container
					.getData().getSGInfo().getSGInterchange());

		tr = new TemporalRender(this.container, container.getData().getSGInfo()
				.getSGInterchange());
	}

	public void reset() {
		tr.reset();
	}

	public TemporalRender getTemporalRender() {
		return tr;
	}
}
